/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package Zoomanagement;

/**
 * Zoo Class - Manages a collection of animals, implementing the ZooAPI interface.
 * @author ashongtical
 */
public class Zoo implements ZooAPI {

    private String name;
    private Animal[] animals;
    private int counter;

    /**
     * Constructs a Zoo with the given name and initial capacity of 10 animals.
     * @param name The name of the zoo.
     */
    public Zoo(String name) {
        this.name = name;
        this.animals = new Animal[10]; // Initial capacity 10
        this.counter = 0;
    }

    /**
     * Adds an animal to the zoo, expanding the array if necessary.
     * @param animal The animal to add.
     */
    @Override
    public void addAnimal(Animal animal) {
        if (counter >= animals.length) {
            Animal[] newArray = new Animal[animals.length * 2];
            System.arraycopy(animals, 0, newArray, 0, animals.length);
            animals = newArray;
        }
        animals[counter++] = animal;
    }

    /**
     * Deletes an animal from the zoo by name.
     * @param name The name of the animal to delete.
     */
    @Override
    public void deleteAnimal(String name) {
        int index = findAnimal(name);
        if (index != -1) {
            for (int i = index; i < counter - 1; i++) {
                animals[i] = animals[i + 1];
            }
            counter--;
        }
    }

    /**
     * Finds an animal in the zoo by name.
     * @param name The name of the animal to find.
     * @return The index of the animal in the collection, or -1 if not found.
     */
    @Override
    public int findAnimal(String name) {
        for (int i = 0; i < counter; i++) {
            if (animals[i].getName().equalsIgnoreCase(name)) {
                return i;
            }
        }
        return -1;
    }

    /**
     * Moves an animal to another zoo with logistics details.
     * @param name The name of the animal to move.
     * @param destination The destination zoo.
     * @param logistics The logistics details for the move.
     */
    @Override
    public void moveAnimal(String name, Zoo destination, Logistics logistics) {
        int index = findAnimal(name);
        if (index != -1) {
            Animal animal = animals[index];
            deleteAnimal(name);
            destination.addAnimal(animal);
            System.out.println("\n--- Logistics Summary ---");
            logistics.display();
        }
    }

    /**
     * Displays all animals in the zoo.
     */
    @Override
    public void displayAnimals() {
        System.out.println("\n=== " + name + " Animals ===");
        for (int i = 0; i < counter; i++) {
            animals[i].display();
        }
    }

    /**
     * Gets the name of the zoo.
     * @return The name of the zoo.
     */
    @Override
    public String getName() {
        return name;
    }

    /**
     * Gets the number of animals in the zoo.
     * @return The count of animals.
     */
    @Override
    public int getCounter() {
        return counter;
    }

    /**
     * Gets an animal at a specific index.
     * @param index The index of the animal.
     * @return The animal at the index, or null if invalid.
     */
    @Override
    public Animal getAnimal(int index) {
        if (index >= 0 && index < counter) {
            return animals[index];
        }
        return null;
    }
}